package code.example.javassist.code;

import code.example.javassist.reflect.ClassReflectorFactory;
import code.example.javassist.reflect.PropertyReflector;

public class PropertyGetterCoder extends Coder{


    String resultVarName = this.createVarName();
    Class resultClassType = null;
    Class objectClass  = null;
    String objectName = null;
    String propertyName = null;

    public PropertyGetterCoder(Class objectClass , String objectName, String propertyName) throws Exception {
        this.objectClass = objectClass;
        this.objectName = objectName;
        this.propertyName = propertyName;
//        PropertyReflector propertyReflector =  ClassReflectorFactory.createClassReflector(objectClass).createBeanPropertyReflector().createPropertyReflector(propertyName);
//        this.resultClassType = propertyReflector.getClassType();
    }

    public String getResultVarName() {
        return resultVarName;
    }

    public Class getResultClassType() {
        return resultClassType;
    }

    public String  write(){
        StringBuilder sb = new StringBuilder();
        String varName = this.createVarName();

        sb.append(objectClass.getName())
                .append(Coder.CODE_SPACE )
                .append(varName)
                .append(Coder.CODE_EQUAL)
                .append(Coder.CODE_LEFT_BRACKET)
                .append(objectClass.getName())
                .append(Coder.CODE_RIGHT_BRACKET)
                .append(objectName)
                .append(Coder.CODE_SEMICOLON)

                .append("\n");
        sb.append(resultClassType.getName())
                .append(Coder.CODE_SPACE)
                .append(this.resultVarName)
                .append(Coder.CODE_SPACE)
                .append(varName)
                .append(Coder.CODE_DOT)
                .append(Coder.CODE_GET)
                .append(Character.toUpperCase(propertyName.charAt(0)))
                .append(propertyName.substring(1))
                .append(Coder.CODE_LEFT_BRACKET)
                .append(Coder.CODE_RIGHT_BRACKET)
                .append(Coder.CODE_SEMICOLON);
       return sb.toString();
    }

}
